package com.opennews.openplatform.service;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import lombok.RequiredArgsConstructor;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class ProductService {
    private final RestTemplate restTemplate;

    @CircuitBreaker(name = "default")
    public List<Map<String, Object>> query(List<String> ids) {
        String uri = UriComponentsBuilder.fromUriString("lb://product-service/api/products")
                .queryParam("ids", ids)
                .toUriString();

        return restTemplate.getForObject(uri, List.class);
    }

//    @GlobalTransactional
    public void testTransaction() {
        restTemplate.postForObject("lb://product-service/api/products", Map.of("id", "id-value"), Void.class);
    }
}